//
//  LLLaunchViewController.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/11.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLLaunchViewController.h"
#import "LLChannelShowCell.h"
#import "LLChannelModel.h"
#import "TabBarViewController.h"
#import "LLMyViewController.h"
#import "LLLoginViewController.h"
#import "LLLaunchReusableView.h"

@interface LLLaunchViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UITabBarControllerDelegate>
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *jinengArr;

@end

@implementation LLLaunchViewController

-(NSMutableArray *)jinengArr{
    if (!_jinengArr) {
        _jinengArr = [NSMutableArray array];
    }
    return _jinengArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    NSData *data = [[GKCacheManager defaultManager] dataFromCachesWithURL:@"all_user_interest"];
    if (data) {
         NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithData:data];
         NSArray *resultArr = [NSArray yy_modelArrayWithClass:[LLChannelModel class] json:dic];
       
        for (LLChannelModel *model in resultArr) {
            if (model.type.integerValue == 1) {
                [self.models addObject:model];
            }else{
                [self.jinengArr addObject:model];
            }
        }
    }else{
        [self getXingQuData];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.view.backgroundColor = UIColor.whiteColor;
    self.navigationController.navigationBar.hidden = YES;
}

//- (void)initData {
//    NSArray *titles =  @[@"新闻",@"体育",@"军事",@"NBA",@"游戏",@"国际",@"科技",@"财经",@"汽车",@"电影",@"美食",@"电视剧",@"新时代",@"两会",@"综艺",@"时尚",@"健康",@"育儿",@"情感"];
//    for (NSInteger i = 0; i < titles.count; i++) {
//        LLChannelModel *model = [LLChannelModel new];
//        model.title = titles[i];
//        model.isSelected = i == 0 ? @"1" : @"0";
//        [self.models addObject:model];
//    }
//    [self.collectionView reloadData];
//}

- (void)initUI {
//    UILabel *noteLbl = [LLInitBaseViewTool labelWithText:@"选择你感兴趣的吧" textColor:LLColorRGB(67) font:18 textAlignment:NSTextAlignmentCenter];
//    noteLbl.frame = CGRectMake(0, 50, LLScreenWidth, 20);
//    [self.view addSubview:noteLbl];
    
    // 确定按钮
    CGFloat left = LLScreenWidth < 375 ? 30 : 40;
    CGFloat btnY = LLScreenHeight < 375 ? LLScreenHeight-self.navH-50 : LLScreenHeight-self.navH-50;
    LLButton *commitBtn = [LLInitBaseViewTool buttonWithTitle:@"确 定" image:nil titleColor:[UIColor whiteColor] font:17];
    commitBtn.frame = CGRectMake(left, btnY, LLScreenWidth-left*2, 38);
    [commitBtn setBackgroundColor:LLColor(225, 0, 0)];
    commitBtn.layer.cornerRadius = 19;
    [commitBtn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitBtn];
    
    UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
    layout.minimumLineSpacing = 15;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(0, left, 0, left);
    layout.itemSize = CGSizeMake((LLScreenWidth-10*3-left*2)/4.0, 35);
     layout.headerReferenceSize = CGSizeMake(LLScreenWidth, 100);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,  10, LLScreenWidth, LLScreenHeight-50-commitBtn.height-50) collectionViewLayout:layout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = UIColor.whiteColor;
    [_collectionView registerClass:[LLChannelShowCell class] forCellWithReuseIdentifier:@"cell"];
    [_collectionView registerClass:[LLLaunchReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];//注册header的view
    [self.view addSubview:_collectionView];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(LLScreenWidth-70, 20, 55, 30);
    [btn setTitle:@"跳过" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(ignore) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}

#pragma mark -
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return section == 0? self.models.count:self.jinengArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LLChannelShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
         cell.launchChannelModel = self.models[indexPath.item];
    }else{
         cell.launchChannelModel = self.jinengArr[indexPath.item];
    }
   
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
     UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    for (UIView *view in headerView.subviews) {
        [view removeFromSuperview];
    }
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, LLScreenWidth, 100)];
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:18];
    label.textAlignment = NSTextAlignmentCenter;
    if (indexPath.section == 0) {
        label.text = @"选择你的兴趣";
    }else{
        label.text = @"选择你的技能";
    }
    [headerView addSubview:label];
    return headerView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        LLChannelModel *model = self.models[indexPath.row];
        model.isSelected = model.isSelected.integerValue ? @"0" : @"1";
    }else{
        LLChannelModel *model = self.jinengArr[indexPath.row];
        model.isSelected = model.isSelected.integerValue ? @"0" : @"1";
    }
    
    [collectionView reloadItemsAtIndexPaths:@[indexPath]];
}


#pragma mark - private method

- (void) ignore
{
    LLLoginViewController *vc = [LLLoginViewController new];
    vc.tagArray = @[];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)commitBtnClick {

//    [self presentViewController:vc animated:YES completion:nil];
    NSMutableArray *tagArr = [NSMutableArray array];
    for (LLChannelModel *model in self.models) {
        if (model.isSelected) {
            NSDictionary *dic =@{@"interestId":model.interestId,@"interest_name":model.interest_name};
            [tagArr addObject:dic];
        }
    }

    NSMutableArray *array = [NSMutableArray array];
    for (LLChannelModel *model in self.jinengArr) {
        if (model.isSelected) {
            NSDictionary *dic =@{@"interestId":model.interestId,@"interest_name":model.interest_name};
            [array addObject:dic];
            [tagArr addObject:dic];
        }
    }

    if (tagArr.count == 0) {
        [self showSVProgressHUDInfoWithsStatus:@"请选择一个标签"];
        return;
    }

    if (array.count < 4) {
        [self showSVProgressHUDInfoWithsStatus:@"技能标签必须选4个以上"];
        return;
    }

//    LLLoginViewController *vc = [LLLoginViewController new];
//    vc.tagArray = tagArr.copy;
//    [self.navigationController pushViewController:vc animated:YES];
    
     [LLUserDefaults setObject:@"1" forKey:@"firstUse"];
     [LLUserDefaults setObject:tagArr.copy forKey:@"user_interest"];
    TabBarViewController *vc = [TabBarViewController new];
    vc.delegate = self;
    [UIApplication sharedApplication].delegate.window.rootViewController = vc;
    
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    
    LLNavViewController *nav = (LLNavViewController *)viewController;
    if ([nav.topViewController isKindOfClass:[LLMyViewController class]]) {
        return [LLUserManager isLogin];
    }
    return YES;
}

//获取兴趣
-(void)getXingQuData{
    //    LLUserInfo *user = [LLUserInfo sharedUser];
    NSDictionary *dic = @{@"edu_id":@"10"};
    
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
//        self.titlesArray = result[@"data"];
        NSArray *resultArr = [NSArray yy_modelArrayWithClass:[LLChannelModel class] json:result[@"data"]];
        [LLUserDefaults setObject:result[@"data"] forKey:@"all_user_interest"];
        NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:result[@"data"]];
        [[GKCacheManager defaultManager] insertCacheWithURL:@"all_user_interest" data:data1];
        for (LLChannelModel *model in resultArr) {
            if (model.type.integerValue == 1) {
                [self.models addObject:model];
            }else{
                [self.jinengArr addObject:model];
            }
        }
     
        [self.collectionView reloadData];
        
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
        
    }];
}

@end
